<?php
    include 'header.php';
    include '../classes/page.class.php';
    /*
        id
        bookname
        publisher
        author
        price
        ptime
        pic
        detail
     */

     if(isset($_GET['action']) && $_GET['action']=="search") {
        //  能够让分页当中，点击下一页，使结果持续有效
                $tmp = !empty($_POST) ? $_POST : $_GET;
         		$whr=array();
         		$args = "";
         		//如果bookname不为空，说明想搜索书名
         		if(!empty($tmp['bookname'])) {
         			$whr[] = "bookname like '%{$tmp['bookname']}%'";

         			$args .= "&bookname={$tmp['bookname']}";
         		}

         		//如果author不为空，说明想搜索作者
         		if(!empty($tmp['author'])) {
         			$whr[] = "author like '%{$tmp['author']}%'";
         			$args .= "&author={$tmp['author']}";
         		}

         		//如果minprice不为空，说明想搜索价格大于多少的
         		if(!empty($tmp['minprice'])) {
         			$whr[] = "price >= '{$tmp['minprice']}'";
         			$args .= "&minprice={$tmp['minprice']}";
         		}

         		//如果maxprice不为空，说明想搜索价格小于多少的图书
         		if(!empty($tmp['maxprice'])) {
         			$whr[] = "price <= '{$tmp['maxprice']}'";
         			$args .= "&maxprice={$tmp['maxprice']}";
         		}


         		if(!empty($whr)) {
         			$where = " where ".implode(" and ", $whr);
         		} else {
         			$where = "";
         		}
     }


     if(isset($_GET['action'])) {
        //  删除动作
         if($_GET['action'] == 'del') {
            //  删除多个
             if(!empty($_POST['id'])) {
                //  在删除前，先select出 需要删除的图片名称，存入数组，删除
                 $sql = "select * from books where id in (".implode(",", $_POST['id']).")";
                 $del_result=$db->query($sql);
                 $del_num=$del_result->num_rows;
                 $del_arr = [];
                 for($i=0;$i<$del_num;$i++) {
                     $del_row = $del_result->fetch_assoc();
                     unlink("../uploads/{$del_row['pic']}");
                     unlink("../uploads/thumb_{$del_row['pic']}");
                 }
                //  删除数据库内容
                 $query = "delete from books where id in (".implode(",", $_POST['id']).")";
             } else {
                 //  在删除前，先select出 需要删除的图片名称，删除
                 $sql = "select * from books where id='{$_GET['id']}'";
                 $del_result=$db->query($sql);
                 $del_num=$del_result->num_rows;
                 $del_row = $del_result->fetch_assoc();
                 unlink("../uploads/{$del_row['pic']}");
                 unlink("../uploads/thumb_{$del_row['pic']}");
                //  删除数据库内容
                 $query = "delete from books where id='{$_GET['id']}'";
             }

             $result=$db->query($query);
             $num_results=$db->affected_rows;

             if($result && $num_results > 0) {
                 echo "{$num_results} delete data successfully";
             } else {
                 echo "{$num_results} failed";
             }
         }
     }
    //  获取总数
     $sql = "select count(*) as total from books {$where}";
     $results = $db -> query($sql);
     $data = $results->fetch_assoc();

    //  创建分页对象
    $page = new Page($data['total'], $num, $args);


    $query = "select * from books {$where} order by id {$page->limit}";
    // 多余的空格会导致无法查询
    $result = $db->query($query);
    $num_results=$result->num_rows;

    // 搜索图书
    echo "搜索图书：";
    	echo '<form action="list.php?action=search" method="post">';
    	echo '按书名:<input type="text" name="bookname" size=8 value="'.$tmp['bookname'].'">&nbsp;&nbsp;';
    	echo '按作者:<input type="text" name="author" size=8  value="'.$tmp['author'].'">&nbsp;&nbsp;';
    	echo '按价格: <input type="text" size="8" name="minprice"  value="'.$tmp['minprice'].'">&nbsp;&nbsp;';
    	echo '- <input type="text" size="8" name="maxprice"  value="'.$tmp['maxprice'].'">&nbsp;&nbsp;';
    	echo '<input type="submit" name="sersubmit" value="搜索">';
    	echo '</form>';


    echo "<form action='list.php?action=del&page=".$page->page."' method='post'id='delmuti'>";
    echo '<table border="1" width="900">';
	echo '<caption><h3>图书列表</h3></caption>';
	echo '<tr>';
	echo '<th>&nbsp;</th>';
	echo '<th>编号</th>';
	echo '<th>图书名称</th>';
	echo '<th>出版社</th>';
	echo '<th>作者</th>';
	echo '<th>价格</th>';
	echo '<th>添加时间</th>';
	echo '<th>操作</th>';
	echo '</tr>';

    for($i=0;$i<$num_results;$i++) {
        $row = $result->fetch_assoc();
        echo '<tr>';
        echo '<td><input type="checkbox" name="id[]" value="'.$row['id'].'"></td>';
        echo '<td>'.$row['id'].'</td>';
        echo '<td>《'.$row['bookname'].'》</td>';
        echo '<td>'.$row['publisher'].'</td>';
        echo '<td>'.$row['author'].'</td>';
        echo '<td>￥'.number_format($row['price'], 2, ".", ",").'</td>';
        echo '<td>'.date("Y-m-d H:i:s",$row['ptime']).'</td>';
        echo '<td><a href="modify.php?action=modify&id='.$row['id'].'">修改</a>/<a
          id="del" href="list.php?action=del'.$args.'&&page='.$page->page.'id='.$row['id'].'">删除</a></td>';
        echo '</tr>';
    }


	echo '<tr><td><input type="submit" value="删除"></td><td colspan="7" align="right">'.$page->fpage().'</td></tr>';

	echo '</table>';
    echo "</form>";

    $result->free();

    include 'footer.php';
